package com.sitanyun.merchant.guide.weiht;

/**
 * Created by jzl
 * on 2022/1/19.
 */
public class StringReplaceUtil {
    /**
     10      * 根据用户名的不同长度，来进行替换 ，达到保密效果
     11      *
     12      * @param userName
     13      *            用户名
     14      * @return 替换后的用户名
     15      */
     public static String userNameReplaceWithStar(String userName) {
                 String userNameAfterReplaced = "";

                if (userName == null) {
                        userName = "";
                    }

                int nameLength = userName.length();
                 System.out.println(nameLength);

                 if (nameLength <= 1) {
                         userNameAfterReplaced = "*";
                     } else if (nameLength == 2) {
                         userNameAfterReplaced = replaceAction(userName, "(?<=\\w{0})\\w(?=\\w{1})");
                    } else if (nameLength >= 3 && nameLength <= 6) {
                         userNameAfterReplaced = replaceAction(userName, "2");
        } else if (nameLength == 7) {
                         userNameAfterReplaced = replaceAction(userName, "(?<=\\d{1})\\d(?=\\d{2})");
                     } else if (nameLength == 8) {
                         userNameAfterReplaced = replaceAction(userName, "(?<=\\d{2})\\d(?=\\d{2})");
                     } else if (nameLength == 9) {
                         userNameAfterReplaced = replaceAction(userName, "(?<=\\d{2})\\d(?=\\d{3})");
                     } else if (nameLength == 10) {
                         userNameAfterReplaced = replaceAction(userName, "(?<=\\d{3})\\d(?=\\d{3})");
                     } else if (nameLength >= 11) {
                         userNameAfterReplaced = replaceAction(userName, "(?<=\\d{3})\\d(?=\\d{4})");
                    } return userNameAfterReplaced;

             }

             /**
 49      * 实际替换动作
 50      *
 51      * @param username
 52      *            username
 53      * @param regular
 54      *            正则
 55      * @return
 56      */
             private static String replaceAction(String username, String regular) {
                 return username.replaceAll(regular, "*");
             }

            /**
 62      * 身份证号替换，保留前四位和后四位
 63      *
 64      * 如果身份证号为空 或者 null ,返回null ；否则，返回替换后的字符串；
 65      *
 66      * @param idCard
 67      *            身份证号
 68      * @return
 69      */
             public static String idCardReplaceWithStar(String idCard) {
                if (idCard.isEmpty() || idCard == null) {
            return null;
                    } else {
                        return replaceAction(idCard, "(?<=\\d{4})\\d(?=\\d{4})");
                    }
             }

            /**
 * 银行卡替换，保留后四位
 *
 * 如果银行卡号为空 或者 null ,返回null ；否则，返回替换后的字符串；
 *
      * @param bankCard
      *            银行卡号
 * @return
             */
            public static String bankCardReplaceWithStar(String bankCard) {
                 if (bankCard.isEmpty() || bankCard == null) {
            return null;
                     } else {
                         return replaceAction(bankCard, "(?<=\\d{0})\\d(?=\\d{4})");
                    }
       }
 }

